package controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CategoriasDao;
import dao.CategoriasDaoImpl;

import model.Categoria;
import model.Pais;

@WebServlet("/categoria")
public class CategoriaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CategoriaServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pais = request.getParameter("paises").toString().trim();
		String cat = request.getParameter("categoria").toString().trim();
		Categoria c = new Categoria();
		Pais p = new Pais();
		p.setId(Integer.parseInt(pais));
		c.setTipo(cat);
		CategoriasDao cDao = new CategoriasDaoImpl();
		String saida = "";
		try {
			saida = cDao.cadastraCategorias(c, p);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("saida", saida);
		RequestDispatcher view = request.getRequestDispatcher("admin.jsp");
		view.forward(request, response);
	}

}
